public class test03 {
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode() {}
     *     TreeNode(int val) { this.val = val; }
     *     TreeNode(int val, TreeNode left, TreeNode right) {
     *         this.val = val;
     *         this.left = left;
     *         this.right = right;
     *     }
     * }
     */
        //还是需要map，因为要确定最大值的索引
        HashMap<Integer,Integer> map;
        public TreeNode constructMaximumBinaryTree(int[] nums) {
            map = new HashMap<>();
            for(int i = 0;i<nums.length;i++){
                map.put(nums[i],i);
            }
            return buildRoot(nums,0,nums.length);
        }
        private TreeNode buildRoot(int[] nums,int begin,int end){
            int max = Integer.MIN_VALUE;
            if(begin>=end){
                return null;
            }
            for(int i = begin;i<end;i++){
                max = Math.max(max,nums[i]);
            }
            int index = map.get(max);
            TreeNode root = new TreeNode(max);
            int lenOfLeft = index-begin;
            root.left = buildRoot(nums,begin,index);
            root.right = buildRoot(nums,index+1,end);
            return root;
        }
}
